package com.yenlien.traceyun.milk.controller;

import com.yenlien.traceyun.milk.controller.vo.PageVo;
import com.yenlien.traceyun.milk.entity.MilkScreen;
import com.yenlien.traceyun.milk.entity.Storage;
import com.yenlien.traceyun.milk.service.MilkScreenService;
import com.yenlien.traceyun.milk.service.StorageService;
import com.yenlien.traceyun.webase.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Date 2022/7/11
 * @Author jiaLong
 * @Descript 交易管理页面
 */
@Api(tags = "交易管理页面")
@RestController
@RequestMapping("/storage")
public class StorageController extends BaseController {

    @Autowired
    private StorageService storageService;

    @Autowired
    private MilkScreenService milkScreenService;

    @PostMapping(value = "/save")
    public Object save(@RequestBody Storage storage) {
        return success(storageService.save(storage));
    }

    @PostMapping(value = "/findByList")
    public Object findByList(@RequestBody Storage storage) {
        return success(storageService.findByList(storage));
    }

    @ApiOperation(("/查询所有收奶量和其总金额,总大屏使用"))
    @GetMapping("/findByInNumAndPrice")
    public Map<String, Object> findByInNumAndPrice() {
        return milkScreenService.findAll();
    }

    @ApiOperation("牧场名称和收奶量倒序")
    @GetMapping("/findPastureSort")
    public Map<String, Object> findPastureSort(Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, "milkYield");
        Page<MilkScreen> pages = milkScreenService.findPastureSort(pageable);
        Map<String, Object> map = new HashMap<>();
        map.put("totalPages", pages.getTotalPages());
        map.put("total", pages.getTotalElements());
        map.put("number", pages.getNumber());
        map.put("data", pages.getContent());
        return map;
    }
}
